import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/basic/address_info.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/widget/asr_app_bar.dart';

int proIndex = 0;
int cityIndex = 0;
int distIndex = -1;
class XianHuoSXAddress extends StatefulWidget {
  final Function callBack;
  final double topSpace;
  final int proIndex;
  const XianHuoSXAddress({Key? key, required this.callBack, required this.topSpace, required this.proIndex}) :  super(key: key);

  @override
  State<XianHuoSXAddress> createState() => _XianHuoSXAddressState();
}

class _XianHuoSXAddressState extends State<XianHuoSXAddress> {
  List provices = [];
  List cityies = [];
  List districts = [];
  // int proIndex = 0;
  // int cityIndex = 0;
  // int distIndex = -1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // proIndex = widget.proIndex ?? 0;
    getAddress();
  }

  getAddress()async{
    Map address = await AddressInfo.getAddress();
    setState(() {
      List arr = address['districts'][0]['districts']??[];
      for (var element in arr) {
        provices.add(element);
      }
      provices.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
      provices.insert(0, {
        'name':'全国货源',
        'districts':[]
      });
      provices.insert(1, {
        'name':'附近货源',
        'districts':[]
      });
      cityies = provices[proIndex]['districts'];
      cityies.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
      if(cityies.isNotEmpty){
        districts = cityies[cityIndex]['districts']??[];
        districts.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.transparent,
      child: Column(
        children: [
          GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
            },
            child: Container(
              height: widget.topSpace,
              color: Colors.transparent,
            ),
          ),
          Container(
            width: 1.sw,
            color: Color(0xffF1F1F4),
            child: Row(
              children: [
                Container(
                  width: (1.sw-2)/3,
                  height: 800.w,
                  child: ListView.builder(
                    padding: EdgeInsets.zero,
                    itemBuilder: (context,index){
                    return GestureDetector(
                      onTap: (){
                        setState(() {
                          proIndex = index;
                          cityIndex = 0;
                          distIndex = -1;
                          cityies = [];
                          districts = [];
                          cityies = provices[proIndex]['districts']??[];
                          if(cityies.isNotEmpty){
                            cityies.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
                            districts = cityies[cityIndex]['districts']??[];
                            districts.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
                          }
                        });
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 20.w),
                        color: proIndex == index? Colors.white:Colors.transparent,
                        alignment: Alignment.center,
                        child: Text('${provices[index]['name']}',maxLines: 1,style: TextStyle(
                          fontSize: 32.sp,
                          color: proIndex!=index? Color(0xff6A6A6A):Color(0xff444444),
                          fontWeight: FontWeight.w400
                        ),),
                      ),
                    );
                  },itemCount: provices.length,),
                ),
                Container(
                  color: Color(0xffdddddd),
                  width: 1,
                  height: 800.w,
                ),
                Container(
                  width: (1.sw-2)/3,
                  height: 800.w,
                  child: ListView.builder(
                    padding: EdgeInsets.zero,
                    itemBuilder: (context,index){
                      return GestureDetector(
                        onTap: (){
                          setState(() {
                            cityIndex = index;
                            districts = cityies[cityIndex]['districts']??[];
                            districts.sort(((a, b) => a['adcode'].compareTo(b['adcode'])));
                            distIndex = -1;
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 20.w),
                          color: cityIndex == index? Colors.white:Colors.transparent,
                          alignment: Alignment.center,
                          child: Text('${cityies[index]['name']}',maxLines: 1,style: TextStyle(
                              fontSize: 32.sp,
                              color: cityIndex!=index? Color(0xff6A6A6A):Color(0xff444444),
                              fontWeight: FontWeight.w400
                          ),),
                        ),
                      );
                    },itemCount: cityies.length,),
                ),
                Container(
                  color: Color(0xffdddddd),
                  width: 1,
                  height: 800.w,
                ),
                Container(
                  width: (1.sw-2)/3,
                  height: 800.w,
                  child: Column(
                    children: [
                      Visibility(child: GestureDetector(
                        onTap: (){
                          setState(() {
                            distIndex = -1;
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 20.w),
                          color: distIndex == -1? Colors.white:Colors.transparent,
                          alignment: Alignment.center,
                          child: Text('全市',maxLines: 1,style: TextStyle(
                              fontSize: 32.sp,
                              color:  distIndex==-1?defaultBarColor: Color(0xff6A6A6A),
                              fontWeight: FontWeight.w400
                          ),),
                        ),
                      ),visible: proIndex>1,),
                      Expanded(child: ListView.builder(
                        padding: EdgeInsets.zero,
                        itemBuilder: (context,index){
                          return GestureDetector(
                            onTap: (){
                              setState(() {
                                distIndex = index;
                              });
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 20.w),
                              color: distIndex == index || distIndex == -1? Colors.white:Colors.transparent,
                              alignment: Alignment.center,
                              child: Text('${districts[index]['name']}',maxLines: 1,style: TextStyle(
                                  fontSize: 32.sp,
                                  color: distIndex==index ? defaultBarColor: distIndex==-1?Color(0xff444444): Color(0xff6A6A6A),
                                  fontWeight: FontWeight.w400
                              ),),
                            ),
                          );
                        },itemCount: districts.length,))
                    ],
                  ),
                )
              ],
            ),
          ),
          Container(),
          Container(
            height: 1,
            color: Color(0xffE9E8E9),
          ),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 20.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: (){
                    if(widget.callBack!=null){
                      widget.callBack(null);
                    }
                    SmartDialog.dismiss();
                  },
                  child: Container(
                    width:350.w,
                    height: 90.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffEBEBEB),
                        borderRadius: BorderRadius.circular(10.w)
                    ),
                    child: Text('重置',style: TextStyle(
                        color: Color(0xff232323),
                        fontSize:30.sp,
                        fontWeight: FontWeight.w500
                    ),),
                  ),
                ),
                GestureDetector(
                  onTap: (){
                    SmartDialog.dismiss();
                    if(widget.callBack!=null){
                      if(proIndex<=1){
                        widget.callBack(provices[proIndex]);
                        return;
                      }
                      if(distIndex==-1){
                        widget.callBack(cityies[cityIndex]);
                      }else{
                        widget.callBack(districts[distIndex]);
                      }
                    }
                  },
                  child: Container(
                    width:320.w,
                    height: 80.w,
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffFA6E28),
                        borderRadius: BorderRadius.circular(10.w)
                    ),
                    child: Text('完成',style: TextStyle(
                        color: Colors.white,
                        fontSize:30.sp,
                        fontWeight: FontWeight.w500
                    ),),
                  ),
                )
              ],
            ),
          ),
          Expanded(child: GestureDetector(
            onTap: (){
              SmartDialog.dismiss();
            },
            child: Container(
              width: 1.sw,
              color: Color.fromRGBO(0, 0, 0, 0.25),
            ),
          ))
        ],
      ),
    );
  }
}
